/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package questionbook.manager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import org.openrdf.rio.RDFFormat;
import questionbook.Configuration.QuestionbookConfiguration;
import questionbook.store.RdfStore;

/**
 * The StoreManager is a Singleton Class which provides the functionality
 * of the RDF-Store which is controlled by the Sesame Framework
 *
 * @author stefanstift
 */
public class StoreManager {

    private static StoreManager instance = null;
    private RdfStore store;
    protected StoreManager()
    {
        // inferencing is done by the owl api
        this.store = new RdfStore(false);
        this.store.addFile(QuestionbookConfiguration.getInstance().getStorePath(), RDFFormat.RDFXML);

        this.store.addNamespace("qbook", QuestionbookConfiguration.getInstance().getBaseURL());
    }

    public static StoreManager getInstance()
    {
        if(instance == null)
            instance = new StoreManager();

        return instance;
    }

    public RdfStore getStore()
    {
        return this.store;
    }

   /**
    * Create a new and up to date internal instance of the Store
    *
    */
    public void reloadStore()
    {

        this.store = new RdfStore(false);
        this.store.addFile(QuestionbookConfiguration.getInstance().getStorePath(), RDFFormat.RDFXML);
        
    }

    /**
     * Serializes the actual Representation of the Store into the File
     * 
     */
    public void flush()
    {
        File f = new File(QuestionbookConfiguration.getInstance().getStorePath());
        FileOutputStream os = null;
        try {

            os = new FileOutputStream(f);
        } catch (FileNotFoundException ex) {
            System.out.println("file exception");
        }
        
        this.store.dumpRDF(os, RDFFormat.RDFXML);
        
    }


}
